<template>
	<view>
		<button class="login-btn" type="primary" open-type="getPhoneNumber" @getphonenumber="getnumber">
			微信用户一键登录3
		</button>
	</view>
</template>
<script lang="ts" setup>
	import { initWebSocket } from "../../utils/ws"
	import { Userlogin } from '../../apis/user'
	import userStore from '../../store/modules/user'
	import { loginStore } from '../../store/modules/login'
	const useStore = userStore();
	const useloginStore = loginStore()
	//获取用户手机号
	const getnumber = async function (e) {
		try {
			console.log(e.detail);

			const res = await Userlogin(
				{
					"code": e.detail.code
				}
			);
			useStore.updateData(res.data.user_id);
			loginSuccess(res.data.token)
		} catch (error) {
			// 错误处理逻辑
			console.error('An error occurred while executing Captcha function:', error);
		}
	}
	//保存token
	const loginSuccess = (profile) => {
		// 保存会员信息
		useloginStore.setProfile(profile)

		// 成功提示
		uni.showToast({
			title: '登录成功',
			icon: 'success',
			mask: true,
		})
		initWebSocket()
		setTimeout(() => {
			// 页面跳转
			// uni.switchTab({ url: '/pages/my/my' })
			uni.navigateBack()
		}, 500)
	}
</script>

<style lang="scss">

</style>